/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SubsitemaAgenda;

import java.awt.Color;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author hernaneleazar
 */
public class DialogoPagoEfectivoConProg extends javax.swing.JDialog {

    private service.Cita cita;
    DecimalFormat df = new DecimalFormat("###,###");
    String idPrograma;

    public DialogoPagoEfectivoConProg(java.awt.Frame parent, boolean modal,service.Cita cita, String idPrograma) {
        super(parent, modal);
        initComponents();
        setIconImage(new ImageIcon(getClass().getResource("/GUI/Img/logo2.png")).getImage());
        this.cita = cita;
        this.idPrograma = idPrograma;
        this.llenarCampos();
        cargarPrestacionesTabla();
    }

    public void cambiarEtiquetaMensajeria(String mensaje, Color color) {
        etiquetaMensajeria.setText(mensaje);
        etiquetaMensajeria.setForeground(color);
    }

    public String calcularFechaActual() {
        String fecha;
        Calendar today = Calendar.getInstance();
        Date currentDate = new Date();
        today.setTime(currentDate);
        fecha = Integer.toString(today.get(Calendar.DAY_OF_MONTH)) + "-"
                + Integer.toString(today.get(Calendar.MONTH) + 1) + "-"
                + Integer.toString(today.get(Calendar.YEAR));
        return fecha;
    }

    public String calcularHoraActual() {
        String hora;
        Calendar now = Calendar.getInstance();
        Date currentDate = new Date();
        now.setTime(currentDate);
        hora = Integer.toString(now.get(Calendar.HOUR)) + ":"
                + Integer.toString(now.get(Calendar.MINUTE) + 1) + ":"
                + Integer.toString(now.get(Calendar.SECOND));
        return hora;
    }

    public void agregarPrestacionTabla(int cantidad, String codigo, String prestacion, String total) {
        DefaultTableModel temp = (DefaultTableModel) tablaPrestaciones.getModel();
        Object nuevo[] = {Integer.toString(cantidad), codigo, prestacion, total, false};
        temp.addRow(nuevo);
    }
    
    public void cargarPrestacionesTabla() {
        java.util.List<service.PrestacionMedica> lista = (java.util.List<service.PrestacionMedica>) listarPrestacionesPorPrograma(idPrograma, cita.getTipoPrograma());
        for (service.PrestacionMedica spres : lista) {
            service.CodigosPrestaciones cod = (service.CodigosPrestaciones) cargarCodigoPrestacion(spres.getCodigo());
            agregarPrestacionTabla(spres.getCantidad(), spres.getCodigo(), cod.getNombrePrestacion(), spres.getValor());
        }
        this.cargarTotal();            
    }
    
    public void cargarTotal(){
        if(cita.getTipoPrograma().equalsIgnoreCase("FONASA")){
            service.ProgramaMedicoFonasa pmf = (service.ProgramaMedicoFonasa)cargarProgramaFonasaPorNumero(idPrograma);
            this.dinero1.getDinero().setText(pmf.getValorTotal());
        }
        else if(cita.getTipoPrograma().equalsIgnoreCase("DIPRECA")){
            service.ProgramaDipreca pd = (service.ProgramaDipreca)obtenerProgramaDipreca(idPrograma);
            this.dinero1.getDinero().setText(pd.getValorTotal());
        }
        else if(cita.getTipoPrograma().equalsIgnoreCase("JEAFOSALE")){
            service.ProgramaMedicoJeafosale pj = (service.ProgramaMedicoJeafosale)obtenerProgramaJeafosale(idPrograma);
            this.dinero1.getDinero().setText(pj.getValorTotal());
        } 
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        etiquetaMensajeria = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        botonPagar = new javax.swing.JButton();
        botonCancelar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaPrestaciones = new javax.swing.JTable();
        jLabel5 = new javax.swing.JLabel();
        dinero1 = new componentes.Dinero();
        jPanel5 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        campoNumeroBoleta = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        spinnerNroSesiones = new javax.swing.JSpinner();
        jPanel6 = new javax.swing.JPanel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        campoNombreProfesional = new javax.swing.JTextField();
        campoRutProfesional = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        campoRutPaciente = new javax.swing.JTextField();
        campoNombrePaciente = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Pago en efectivo");
        setModal(true);

        etiquetaMensajeria.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        etiquetaMensajeria.setText("Área de mensajería");

        jLabel11.setText("* Campos obligatorios");

        botonPagar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Finanzas.png"))); // NOI18N
        botonPagar.setText("Pagar");
        botonPagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonPagarActionPerformed(evt);
            }
        });

        botonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GUI/Img/Iconos16X16/Cancelar.png"))); // NOI18N
        botonCancelar.setText("Cancelar");
        botonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCancelarActionPerformed(evt);
            }
        });

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Prestaciones"));

        tablaPrestaciones.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Cantidad", "Código", "Prestación", "Total"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaPrestaciones);

        jLabel5.setText("Por pagar:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dinero1, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel5)
                    .addComponent(dinero1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos pago en efectivo"));

        jLabel2.setText("N° de Boleta:");

        jLabel3.setText("*");

        jLabel8.setText("N° Sesiones:");

        spinnerNroSesiones.setModel(new javax.swing.SpinnerNumberModel(1, 1, 99, 1));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(spinnerNroSesiones)
                    .addComponent(campoNumeroBoleta, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(campoNumeroBoleta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spinnerNroSesiones, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 15, Short.MAX_VALUE))
        );

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos profesional"));

        jLabel9.setText("Rut:");

        jLabel10.setText("Nombre:");

        campoNombreProfesional.setEnabled(false);

        campoRutProfesional.setEnabled(false);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(campoNombreProfesional, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
                    .addGroup(jPanel6Layout.createSequentialGroup()
                        .addComponent(campoRutProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(campoRutProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNombreProfesional, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos paciente"));

        jLabel1.setText("Rut:");

        campoRutPaciente.setEnabled(false);

        campoNombrePaciente.setEnabled(false);

        jLabel6.setText("Nombre:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(campoRutPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 114, Short.MAX_VALUE))
                    .addComponent(campoNombrePaciente, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoRutPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(campoNombrePaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(etiquetaMensajeria, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel11)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(botonPagar, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(botonCancelar)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(botonPagar)
                        .addComponent(botonCancelar))
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 8, Short.MAX_VALUE)
                .addComponent(etiquetaMensajeria)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonPagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonPagarActionPerformed
        if (camposObligatoriosLlenos()) {
            this.dispose();
            String fecha = calcularFechaActual();
            String hora = calcularHoraActual();
            String nroBoleta = campoNumeroBoleta.getText();
            String nombrePac = campoNombrePaciente.getText();
            String rutPac = campoRutPaciente.getText();
            String nombreProf = campoNombreProfesional.getText();
            String rutProf = campoRutProfesional.getText();
            String nSesiones = spinnerNroSesiones.getValue().toString();
            String valorTotal = dinero1.getDinero().getText();
            
            if (cita != null) {
                int valorIngresado = Integer.parseInt(valorTotal.replace("$", "").replace(".", "")); 
                if (cita.getTipoPrograma().equalsIgnoreCase("FONASA")) {
                    service.ProgramaMedicoFonasa pmf = (service.ProgramaMedicoFonasa) cargarProgramaFonasaPorNumero(idPrograma);
                    if (pmf != null) {
                        int valorPrograma = Integer.parseInt(pmf.getValorTotal().replace("$", "").replace(".", ""));
                        if(valorPrograma >= valorIngresado){
                            this.dispose();
                            agregarPagoEfectivo(cita.getId(), fecha, hora, nombrePac, rutPac, nombreProf, rutProf, nSesiones, nroBoleta, valorTotal);
                            if(valorPrograma == valorIngresado){
                                pmf.setPagado(true);
                                cita.setEstado("Pagada");
                                modificarCita(cita);
                            }
                            int valorNuevo = valorPrograma - valorIngresado;
                            pmf.setValorTotal("$"+df.format(valorNuevo));
                            modificarProgramaFonasa(pmf);
                        }
                        else if(valorPrograma < valorIngresado){
                            this.cambiarEtiquetaMensajeria("El valor ingresado es mayor que el valor del programa", Color.red);
                        }
                        
                    }
                } else if (cita.getTipoPrograma().equalsIgnoreCase("DIPRECA")) {
                    service.ProgramaDipreca pd = (service.ProgramaDipreca) obtenerProgramaDipreca(idPrograma);
                    if (pd != null) {
                        int valorPrograma = Integer.parseInt(pd.getValorTotal().replace("$", "").replace(".", ""));
                        if(valorPrograma >= valorIngresado){
                            this.dispose();
                            agregarPagoEfectivo(cita.getId(), fecha, hora, nombrePac, rutPac, nombreProf, rutProf, nSesiones, nroBoleta, valorTotal);
                            if(valorPrograma == valorIngresado){
                                pd.setPagado(true);
                                cita.setEstado("Pagada");
                                modificarCita(cita);
                            }
                            int valorNuevo = valorPrograma - valorIngresado;
                            pd.setValorTotal("$"+df.format(valorNuevo));
                            editarProgramaDipreca(pd);
                        }
                        else if(valorPrograma < valorIngresado){
                            this.cambiarEtiquetaMensajeria("El valor ingresado es mayor que el valor del programa", Color.red);
                        }                        
                    }
                } else if (cita.getTipoPrograma().equalsIgnoreCase("JEAFOSALE")) {
                    service.ProgramaMedicoJeafosale pj = (service.ProgramaMedicoJeafosale) obtenerProgramaJeafosale(idPrograma);
                    if (pj != null) {
                        int valorPrograma = Integer.parseInt(pj.getValorTotal().replace("$", "").replace(".", ""));
                        if(valorPrograma >= valorIngresado){
                            this.dispose();
                            agregarPagoEfectivo(cita.getId(), fecha, hora, nombrePac, rutPac, nombreProf, rutProf, nSesiones, nroBoleta, valorTotal);
                            if(valorPrograma == valorIngresado){
                                pj.setPagado(true);
                                cita.setEstado("Pagada");
                                modificarCita(cita);
                            }
                            int valorNuevo = valorPrograma - valorIngresado;
                            pj.setValorTotal("$"+df.format(valorNuevo));
                            editarProgramaJeafosale(pj);
                        }
                        else if(valorPrograma < valorIngresado){
                            this.cambiarEtiquetaMensajeria("El valor ingresado es mayor que el valor del programa", Color.red);
                        }                        
                    }
                }
            }
        } else {
            cambiarEtiquetaMensajeria("Debe llenar los campos obligatorios", Color.red);
        }
    }//GEN-LAST:event_botonPagarActionPerformed

    private void botonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCancelarActionPerformed

    public boolean camposObligatoriosLlenos() {
        if (!campoNumeroBoleta.getText().isEmpty() && !dinero1.getDinero().getText().isEmpty()) {
            return true;
        }
        return false;
    }

    public void llenarCampos() {
        service.Paciente p = (service.Paciente) cargarPaciente(cita.getRutPaciente());
        service.Usuario u = (service.Usuario) cargarUsuario(cita.getRutProfesional());
        this.campoNombrePaciente.setText(p.getNombres() + " " + p.getApellidoPaterno() + " " + p.getApellidoMaterno());
        this.campoRutPaciente.setText(cita.getRutPaciente());
        this.campoRutProfesional.setText(cita.getRutProfesional());
        this.campoNombreProfesional.setText(u.getNombres() + " " + u.getApellidoPaterno() + " " + u.getApellidoMaterno());
    }

    public void normalizarMensajeria() {
        etiquetaMensajeria.setText("Área de mensajería");
        etiquetaMensajeria.setForeground(Color.black);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCancelar;
    private javax.swing.JButton botonPagar;
    private javax.swing.JTextField campoNombrePaciente;
    private javax.swing.JTextField campoNombreProfesional;
    private javax.swing.JTextField campoNumeroBoleta;
    private javax.swing.JTextField campoRutPaciente;
    private javax.swing.JTextField campoRutProfesional;
    private componentes.Dinero dinero1;
    private javax.swing.JLabel etiquetaMensajeria;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSpinner spinnerNroSesiones;
    private javax.swing.JTable tablaPrestaciones;
    // End of variables declaration//GEN-END:variables

    private static Object buscarCitaPorId(java.lang.Long id) {
        service.AgendaWS_Service service = new service.AgendaWS_Service();
        service.AgendaWS port = service.getAgendaWSPort();
        return port.buscarCitaPorId(id);
    }

    private static boolean modificarCita(service.Cita arg0) {
        service.AgendaWS_Service service = new service.AgendaWS_Service();
        service.AgendaWS port = service.getAgendaWSPort();
        return port.modificarCita(arg0);
    }

    private static Long registrar(service.Usuario usuario, java.lang.String modulo, java.lang.String accion, java.lang.String valorAnterior, java.lang.String valorNuevo) {
        service.BitacoraWSService service = new service.BitacoraWSService();
        service.BitacoraWS port = service.getBitacoraWSPort();
        return port.registrar(usuario, modulo, accion, valorAnterior, valorNuevo);
    }

    private static Object cargarPaciente(java.lang.String rut) {
        service.PacienteWS_Service service = new service.PacienteWS_Service();
        service.PacienteWS port = service.getPacienteWSPort();
        return port.cargarPaciente(rut);
    }

    private static Object cargarUsuario(java.lang.String rut) {
        service.UsuarioWSService service = new service.UsuarioWSService();
        service.UsuarioWS port = service.getUsuarioWSPort();
        return port.cargarUsuario(rut);
    }

    private static Object cargarCodigoPrestacion(java.lang.String id) {
        service.PrestacionMedicaGeneralWSService service = new service.PrestacionMedicaGeneralWSService();
        service.PrestacionMedicaGeneralWS port = service.getPrestacionMedicaGeneralWSPort();
        return port.cargarCodigoPrestacion(id);
    }

    private static boolean agregarPagoEfectivo(java.lang.Long idCita, java.lang.String fecha, java.lang.String hora, java.lang.String nombresPaciente, java.lang.String rutPaciente, java.lang.String nombresProfesional, java.lang.String rutProfesional, java.lang.String nroSesiones, java.lang.String nroBoleta, java.lang.String valor) {
        service.PagoWS_Service service = new service.PagoWS_Service();
        service.PagoWS port = service.getPagoWSPort();
        return port.agregarPagoEfectivo(idCita, fecha, hora, nombresPaciente, rutPaciente, nombresProfesional, rutProfesional, nroSesiones, nroBoleta, valor);
    }

    private static Object obtenerProgramaDipreca(java.lang.String numero) {
        service.ProgramaDiprecaWSService service = new service.ProgramaDiprecaWSService();
        service.ProgramaDiprecaWS port = service.getProgramaDiprecaWSPort();
        return port.obtenerProgramaDipreca(numero);
    }

    private static boolean editarProgramaDipreca(service.ProgramaDipreca programaDipreca) {
        service.ProgramaDiprecaWSService service = new service.ProgramaDiprecaWSService();
        service.ProgramaDiprecaWS port = service.getProgramaDiprecaWSPort();
        return port.editarProgramaDipreca(programaDipreca);
    }

    private static Object obtenerProgramaJeafosale(java.lang.String numero) {
        service.ProgramaJeafosaleWSService service = new service.ProgramaJeafosaleWSService();
        service.ProgramaJeafosaleWS port = service.getProgramaJeafosaleWSPort();
        return port.obtenerProgramaJeafosale(numero);
    }

    private static boolean editarProgramaJeafosale(service.ProgramaMedicoJeafosale programaDipreca) {
        service.ProgramaJeafosaleWSService service = new service.ProgramaJeafosaleWSService();
        service.ProgramaJeafosaleWS port = service.getProgramaJeafosaleWSPort();
        return port.editarProgramaJeafosale(programaDipreca);
    }

    private static Object cargarProgramaFonasaPorNumero(java.lang.String id) {
        service.ProgramaFonasaWSService service = new service.ProgramaFonasaWSService();
        service.ProgramaFonasaWS port = service.getProgramaFonasaWSPort();
        return port.cargarProgramaFonasaPorNumero(id);
    }

    private static boolean modificarProgramaFonasa(service.ProgramaMedicoFonasa programaMedico) {
        service.ProgramaFonasaWSService service = new service.ProgramaFonasaWSService();
        service.ProgramaFonasaWS port = service.getProgramaFonasaWSPort();
        return port.modificarProgramaFonasa(programaMedico);
    }
    
    private static java.util.List<service.PrestacionMedica> listarPrestacionesPorPrograma(java.lang.String numeroPrograma, java.lang.String tipoPrograma) {
        service.PrestacionMedicaGeneralWSService service = new service.PrestacionMedicaGeneralWSService();
        service.PrestacionMedicaGeneralWS port = service.getPrestacionMedicaGeneralWSPort();
        return port.listarPrestacionesPorPrograma(numeroPrograma, tipoPrograma);
    }
}
